package com.example.studentmanagersystem;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.fs.utils.CmdUtils;

/**
 * @version 1.0
 * @author cwf
 * data 2015-11-12
 *
 */
public class StudentInfoChangeActivity extends Activity {
	
	private EditText nameET,passwordET,ageET,phoneET;
	private RadioGroup sexRadio;
	private Button submit;
	private Button actionBack,actionMenu;
	 
	
	private String name;
	private String password;
	private int age;
	private String phone;
	private String sex;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.student_personal_infochange_activity);
		
		getInfoStateBitmap();
		this.init();
		
	}
	
	
	/**
	 * initialize widget
	 */
	private void init() {
		
		this.nameET=(EditText) this.findViewById(R.id.student_info_name_et);
		this.passwordET=(EditText) this.findViewById(R.id.student_info_password_et);
		this.ageET=(EditText) this.findViewById(R.id.student_info_age_et);
		this.phoneET=(EditText) this.findViewById(R.id.student_info_phone_et);
		this.submit=(Button) this.findViewById(R.id.student_info_updateSubmit);
		this.sexRadio=(RadioGroup) this.findViewById(R.id.student_info_sexRadio);
		
		//Sex RadioButton
		this.sexRadio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				 
				if(checkedId==R.id.student_info_sex_nam){
					
					sex="nam";
					
				}else if(checkedId==R.id.student_info_sex_women){
					
					sex="women";
					
				}
			}
		});
		
		//Submit change info
		this.submit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {

				getChangeInfo();//invoking getChangeInfo method
				
			}
		});
		
		/**
		 * ActionBar back Button
		 */
		this.actionBack=(Button) this.findViewById(R.id.stuInfo_action_backButton);
		this.actionBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				StudentInfoChangeActivity.this.finish();
				
			}
		});
		
		/**
		 * ActionBar Menu
		 */
		this.actionMenu=(Button) this.findViewById(R.id.stuInfo_action_menu);
		this.actionMenu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				PopupMenuUtil popupMenuUtil=new PopupMenuUtil(v, StudentInfoChangeActivity.this);
				popupMenuUtil.actionBackListener();
				
			}
		});
	}
	
	
	
	
	/**
	 * get the change user info method
	 */
	private void getChangeInfo() {
		
		TextView nameState=(TextView) this.findViewById(R.id.stu_nameState_tv);
		 
		boolean isName=TextUtils.isEmpty(nameET.getText());
		boolean isPassword=TextUtils.isEmpty(passwordET.getText());
		boolean isAge=TextUtils.isEmpty(ageET.getText());
		boolean isPhone=TextUtils.isEmpty(phoneET.getText());
		boolean isSex=TextUtils.isEmpty(sex);
		
		
		if(isName){
			
		}else if(!(isName && isPassword && isAge && isPhone && isSex)){
			
			this.name=nameET.getText().toString().trim();
			this.password=passwordET.getText().toString().trim();
			this.age=Integer.parseInt(ageET.getText().toString().trim());
			this.phone=phoneET.getText().toString().trim();
			
			//show Dialog
			showNormalDia();
		}

	}
	
	
	private Bitmap rightBitmap;
	private Drawable rightDrawable;
	
	private Bitmap wrongBitmap;
	private Drawable wrongDrawable;
	
	/**
	 * change info success or fail,that will show state
	 */
	private void getInfoStateBitmap() {
		
		this.rightBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.info_right);
		this.rightDrawable = new BitmapDrawable(this.getResources(), rightBitmap);
		
		this.wrongBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.info_wrong);
		this.wrongDrawable = new BitmapDrawable(this.getResources(), wrongBitmap);


	}
	
	
	
	/**
	 * show dialog,prompt update info
	 */
	  private void showNormalDia(){  
	        //AlertDialog.Builder normalDialog=new AlertDialog.Builder(getApplicationContext());  
	        AlertDialog.Builder normalDia=new AlertDialog.Builder(this);  
	        normalDia.setIcon(R.drawable.sure);  
	        normalDia.setTitle("提示");  
	        normalDia.setMessage("确定添加？");  
	          
	        normalDia.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
	            @Override  
	            public void onClick(DialogInterface dialog, int which) {  
	                
	            	String cmd="update stu set name='"+name+"',password='"+password+"',age="+age+",sex='"+sex+"',tel='"+phone+"' where _id="+MainActivity._id+"";
	    			
	    			CmdUtils cmdUtils=new CmdUtils(StudentInfoChangeActivity.this);
	    			
	    			cmdUtils.submitCmd(URL.CMD_URL,"update",cmd);
	    			
	    			StudentInfoChangeActivity.this.finish();
	            }  
	        });  
	        
	        normalDia.setNegativeButton("取消", new DialogInterface.OnClickListener() {  
	            @Override  
	            public void onClick(DialogInterface dialog, int which) {  
	               
	            }  
	        });  
	        normalDia.create().show();  
	    } 
	
	
	
	
	
	
	
	
}
